<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/headeradmin.php");
?>

<section>
<hgroup>
<h1>Onderdeel-probleem toevoegen</h1>
</hgroup>
<?php if(!isset($_POST['Submit'])) { 
?>
<form  name="kamerform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
    <tr>
        <td>Kamer:</td>
        <td>
            <?php
                $sql1 = "SELECT * from roomitem WHERE Id=".$_GET['id']."";
                $result1 = mysql_query($sql1) or die (mysql_error());
                while($row1 = mysql_fetch_array($result1))
                {
                    $sql2 = "SELECT * from roomtype WHERE Id=".$row1['RoomId']."";
                    $result2 = mysql_query($sql2) or die (mysql_error());
                    while($row2 = mysql_fetch_array($result2))
                    {
                        echo "<input name=\"kamer\" value=\"".$row2['Name']."\" disabled type=\"text\">";
                    }
                }
                
                
            ?>
        </td>
    </tr>
    <tr>
        <td>Onderdeel:</td>
        <td>
            <?php
                // onderdeel uit tabel "roomitem" halen gebaseerd op het id wat mee wordt gegeven uit hoofdprobleem
                $sql3 = "SELECT * from roomitem WHERE Id=".$_GET['id']."";
                $result3 = mysql_query($sql3) or die (mysql_error());
                while($row3 = mysql_fetch_array($result3))
                {
                    echo "<input name=\"onderdeel\" value=\"".$row3['Name']."\" disabled type=\"text\">";
                }
            ?>
        </td>
    </tr>
    <tr>
        <td>Onderdeel-probleem:</td>
        <td><input required="required" placeholder="Probleem (bv: klink)" name="probleem" type="text"></td>
    </tr>
    <tr>
        <td>Extra tekst kunnen ingeven:</td>
        <td>
            <div class="onoffswitch">
                <input type="checkbox" name="extratekst" class="onoffswitch-checkbox" id="extratekst">
                <label class="onoffswitch-label" for="extratekst">
                    <div class="onoffswitch-inner">
                        <div class="onoffswitch-active"><div class="onoffswitch-switch">JA</div></div>
                        <div class="onoffswitch-inactive"><div class="onoffswitch-switch">NEE</div></div>
                    </div>
                </label>
            </div>
        </td>
    </tr>
    <tr>
        <td>Geactiveerd:</td>
        <td>
            <div class="onoffswitch">
                <input type="checkbox" name="activatie" class="onoffswitch-checkbox" id="activatie" checked>
                <label class="onoffswitch-label" for="activatie">
                    <div class="onoffswitch-inner">
                        <div class="onoffswitch-active"><div class="onoffswitch-switch">JA</div></div>
                        <div class="onoffswitch-inactive"><div class="onoffswitch-switch">NEE</div></div>
                    </div>
                </label>
            </div>
        </td>
    </tr>
    <tr>
        <td><input type="submit" name="Submit" value="Toevoegen">
            <input type="hidden" name="id" value="<?php echo $_GET['id'] ?>" />
            <input type="hidden" name="kamer" value="<?php echo $_GET['kamer'] ?>" />
        </td>
        <td><input type="button" value="Terug" onclick="javascript:history.back();" /></td>
    </tr>
</table>
</form>
<?php
} // einde isset submit
else 
{	
	$error = array();
	
        if (isset($_POST['extratekst']))
        {
            $extratekst = "T";
        }
        else
        {
            $extratekst = "F";
        }

        
        if (isset($_POST['activatie']))
        {
            $activatie = "T";
        }
        else
        {
            $activatie = "F";
        }
        
        if($_POST['probleem'] == "") {
		$error[] = 'Je moet een probleem invullen.';
	}
        
       $errors = sizeof($error); // aantal errors tellen
        if($errors != 0) { // Er is minstens 1 error
                echo "<span style=\"color:red\">Er kon geen onderdeel-probleem worden toegevoegd omwille van de volgende reden(en):</span>";
                echo "<ul>";
                        for($i = 0; $i < $errors; $i++) {
                                echo "<span style=\"color:red\"><li>".$error[$i]."</li></span>";
                        }
                echo "</ul>";
        } 
        else 
        {
            mysql_query("INSERT INTO itemproblem (ItemId, ParentId, Name, Value, Activated) VALUES ('".$_POST['id']."','-1','".$_POST['probleem']."','".$extratekst."','".$activatie."')");
            echo "Het onderdeel is toegevoegd.<br>";
            echo "Klik <a href=\"onderdeelprobleem.php?kamer=".$_POST['kamer']."&onderdeel=".$_POST['id']."\">hier</a> om terug te gaan naar het overzicht.";
        }
} 
?>    
    
    
</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>